package com.huangliusong.lengthoflongestsubstring;

import java.util.HashSet;
import java.util.Set;

/**
 * @author huangliusong
 * @since 2019/2/21
 * {@link HashSet}
 */
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int ans=0;
        int n=s.length();
        for (int i=0;i<n-1;i++){
            for (int j=i;j<n;j++){
                boolean flag=allUnique(s,i,j);
                if(flag){
                    ans=Math.max(ans,j-i);
                }
            }
        }
        return ans;
    }

    public boolean allUnique(String s, int start, int end) {
        Set<Character> set=new HashSet<Character>();
        for (int i=start;i<end;i++){
            if(set.contains(s.charAt(i))){
                return false;
            }
            set.add(s.charAt(i));
        }
        //System.out.println(set.toString()+"   "+"["+start+"-"+end+"]");
        return true;
    }
}
